/**
 * 请假相关
 */

var mongoose = require('mongoose');
var Holiday = mongoose.model('Holiday');

// 提交请假
exports.addHoliday = function (req, res) {

    var begin_time = req.body.begin_time; // 开始时间
    var end_time = req.body.end_time; // 结束时间
    var reason = req.body.reason; // 请假理由
    var user_name = req.body.user_name // 用户

    var newHoliday = new Holiday({
        begin_time: begin_time,
        end_time: end_time,
        reason: reason,
        user_name: user_name,
        feedback: "0"
    });

    newHoliday.save(function (err) {
        if (err) {
            res.json({
                "status": "error"
            })
        } else {
            res.json({
                "status": "success"
            });
        }
    });
}

// 查询请假（分页查询）
exports.getHolidays = function (req, res) {

    var user_name = req.body.user_name;
    var page_num = req.body.page_num; // 从1开始，每页5条

    console.log("当前页码：" + page_num);

    Holiday.find({
        user_name: user_name
    }).sort({
        '_id': -1
    }).limit(5).skip((page_num - 1) * 5).exec(function (err, doc) {
        if (err) {
            res.json({
                "status": "error",
                "msg": "error_system"
            });
        } else {
            if (doc.isNull) {
                res.json({
                    "status": "success",
                    "msg": "data_empty"
                });
            }
            res.json({
                "status": "success",
                "msg": doc
            });
            console.log('---请假记录---');
            // console.log(doc.toString());
        }
    })
}